<template>
    <div class="new_detail">
        <myNav :twoMenu="true"/>

        <div class="new_detail_container">
            <div class="new_header">
                <div class="new_title">{{newDetail.title}}</div>
                <div class="new_date">{{newDetail.date}}</div>
            </div>
            <div id="article_container">

            </div>

            <div class="pre" @click="prePage">上一个：{{preTitle}}</div>
            <div class="next" @click="nextPage">下一个：{{nextTtitle}}</div>
        </div>

        <bottom/>
        <contact/>
    </div>
</template>

<script>
import articleData from "@/assets/js/article";
import Vue from 'vue'
export default {
    name:'newDetail',
    data(){
        return {
            newDetail:{},
            preTitle:'',
            nextTtitle:'',
            index:0,
        }
    },
    mounted() {
        this.init()
    },
    methods: {
        init(){
            let {id} = this.$route.query
            this.index = articleData.findIndex(ele => ele.id == id)
            this.newDetail = articleData[this.index]
            this.appendNode(this.newDetail)
        },
        appendNode(item){
            this.preTitle = this.index-1 < 0 ? '暂无' : articleData[this.index-1].title
            this.nextTtitle =  this.index + 1 > articleData.length -1 ? '暂无' : articleData[this.index+1].title
            let tempNode = Vue.extend({
                template:item.detail
            }) 
            new tempNode().$mount('#article_container')

        },
        toOther(){
            this.$router.push({
                path:'/newDetail',
                query:{
                    id:this.newDetail.id,
                    currentPageName:'新闻中心'
                }
            })
        },
        prePage(){
            if(this.index <= 0) return
            this.newDetail = articleData[this.index-1]
            this.index = this.index - 1
            this.appendNode(this.newDetail)   
            this.toOther()         
        },
        nextPage(){
            if(this.index >= articleData.length - 1) return 
            this.newDetail = articleData[this.index+1]
            this.index = this.index + 1
            this.appendNode(this.newDetail)    
            this.toOther()        
        }
    }


}
</script>

<style lang="scss">
#article_container{
    font-family: PingFang SC-Light, PingFang SC;
    padding:20px 20px 10px;
    p{
        line-height: 30px;
        &::before{
            content:"      ";
            white-space: pre;
        }
    }   
    .p_img{
        width:326px;
        height:384px;
        &::before{
            display: none;
        }
        img{
            width:100%;
            height: 100%;
        }
    } 
}
</style>

<style lang="scss" scoped>
.new_detail_container{
    .new_header{
        padding-bottom: 10px;
        border-bottom: 1px solid #666;
        .new_title{
            text-align: center;
            font-size:20px;
        }
        .new_date{
            text-align:center;
            color:#666;
        }
        // .detail_container{
        //     padding:0 10px;
        // }        
    }
    .pre,.next{
        padding:6px;
        font-size:12px;
        background-color:#f5f5f5;
        margin-top: 1px;
    }
}
</style>